package StacksAndQueues;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class lc20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Character, Character> map = new HashMap<>();
        map.put('(',')');
        map.put('{', '}');
        map.put('[', ']');
        char[] sarr = s.toCharArray();
        for (char c: sarr) {
            if (stack.isEmpty()){
                stack.push(c);
            } else {
                if (map.containsKey(stack.peek()) && map.get(stack.peek()).equals(c)) {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
        }
        return stack.isEmpty();
    }
}